package com.example.amoxicilin.null_test.main;


import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.amoxicilin.null_test.R;
import com.example.amoxicilin.null_test.domain.Status;
import com.example.amoxicilin.null_test.tools.CodeTools;
import com.example.amoxicilin.null_test.tools.HttpUtils;
import com.example.amoxicilin.null_test.tools.JsonTools;

import java.io.UnsupportedEncodingException;

public class PassWordActivity extends AppCompatActivity {

    private EditText password;
    private EditText repeat;
    private Button confirm;
    private EditText oPassword;
    String content="";
    String Pswd = "";
    int number;
    String originPswd = ""; //原来的密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pass_word);

        Bundle bundle=new Bundle();
        bundle=getIntent().getExtras();
        number=bundle.getInt("number");
        originPswd=bundle.getString("password");
        //Toast.makeText(this,originPswd,Toast.LENGTH_SHORT).show();
        password = (EditText) findViewById(R.id.et_newPs);
        repeat = (EditText) findViewById(R.id.et_rpNewPs);
        oPassword = (EditText) findViewById(R.id.et_oldPs);
        confirm = (Button) findViewById(R.id.confirm);

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String oPswd = oPassword.getText().toString();

                if (oPswd.equals("")){
                    final AlertDialog alertDialog = new AlertDialog.Builder(PassWordActivity.this).create();
                    alertDialog.setCancelable(false);
                    alertDialog.setTitle("错误");
                    alertDialog.setMessage("旧密码不能为空");
                    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            alertDialog.cancel();
                            oPassword.setText("");//清空输入框
                            password.setText("");
                            repeat.setText("");
                        }
                    });
                    alertDialog.show();

                }  else if (!oPswd.equals(originPswd)){
                    final AlertDialog alertDialog = new AlertDialog.Builder(PassWordActivity.this).create();
                    alertDialog.setCancelable(false);
                    alertDialog.setTitle("错误");
                    alertDialog.setMessage("旧密码输入不正确");
                    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            alertDialog.cancel();
                            oPassword.setText("");//清空输入框
                            password.setText("");
                            repeat.setText("");

                        }
                    });
                    alertDialog.show();
                }  else if(oPswd.equals(originPswd)) {
                    //Do nothing
                }


                String rPswd = "";

                Pswd = password.getText().toString();
                rPswd = repeat.getText().toString();
                if(Pswd.equals("")||rPswd.equals("")) {
                    final AlertDialog alertDialog = new AlertDialog.Builder(PassWordActivity.this).create();
                    alertDialog.setCancelable(false);
                    alertDialog.setTitle("错误");
                    alertDialog.setMessage("新密码不能为空");
                    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            alertDialog.cancel();
                            password.setText(""); //清空输入框
                            repeat.setText("");
                        }
                    });
                    alertDialog.show();

                } else if (!Pswd.equals(rPswd)) {
                    final AlertDialog alertDialog = new AlertDialog.Builder(PassWordActivity.this).create();
                    alertDialog.setCancelable(false);
                    alertDialog.setTitle("错误");
                    alertDialog.setMessage("两次密码输入不一致");
                    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            alertDialog.cancel();
                            password.setText(""); //清空输入框
                            repeat.setText("");
                        }
                    });
                    alertDialog.show();
                } else if(Pswd.equals(rPswd)) {

                    new android.app.AlertDialog.Builder(PassWordActivity.this)
                            .setTitle("系统消息")
                            .setMessage("确定要提交吗？")
                            .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    try {
                                        content = CodeTools.UTF8Encoder(Pswd.toString());
                                    } catch (UnsupportedEncodingException e) {
                                        e.printStackTrace();
                                    }
                                    String path = "http://120.24.94.172/JsonTestServer/jsonTest?type1=modify&type2=password&password="+content+"&number="+number;
                                    String jsonString = HttpUtils.getJsonContent(path);
                                    Status status= JsonTools.getStatus(null,jsonString);
                                    if (status.getStatusId()==1)
                                    {
                                        new android.app.AlertDialog.Builder(PassWordActivity.this)
                                                .setTitle("系统消息")
                                                .setMessage("修改成功！").setPositiveButton("确认",new DialogInterface.OnClickListener(){
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                finish();
                                            }
                                        }).show();
                                    }
                                    else{
                                        new android.app.AlertDialog.Builder(PassWordActivity.this)
                                                .setTitle("系统消息")
                                                .setMessage("修改失败！").setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                            }
                                        }).show();
                                    }
                                }})
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                }
                            })
                            .show();
                }
            }
        });

    }
}
